<template>
    <view class="address-container">
        <!-- 顶部导航栏 -->
        <view class="header">
            <view class="back-button" @click="goBack">
            </view>
            <view class="page-title">管理地址</view>
            <view class="placeholder"></view>
        </view>

        <!-- 地址列表 -->
        <view class="address-list" v-if="addresses.length > 0">
            <view class="address-item" v-for="(address, index) in addresses" :key="index">
                <view class="address-content">
                    <view class="address-header">
                        <text class="address-name">{{ address.name }}</text>
                        <text class="address-phone">{{ address.phone }}</text>
                    </view>
                    <view class="address-detail">{{ address.detail }}</view>
                    <view class="address-tag" v-if="address.isDefault">默认</view>
                </view>
                <view class="address-actions">
                    <view class="action-item" @click="editAddress(address.id)">
                        <text class="iconfont icon-edit action-icon"
                            :style="{ color: theme.textGray, fontSize: '28rpx' }"></text>
                        <text class="action-text">编辑</text>
                    </view>
                    <view class="action-item" @click="deleteAddress(address.id)">
                        <text class="iconfont icon-delete action-icon"
                            :style="{ color: theme.textGray, fontSize: '28rpx' }"></text>
                        <text class="action-text">删除</text>
                    </view>
                </view>
            </view>
        </view>

        <!-- 空状态 -->
        <view class="empty-state" v-else>
            <view class="empty-icon">📭</view>
            <view class="empty-text">暂无地址，请添加新地址</view>
        </view>

        <!-- 添加地址按钮 -->
        <view class="add-button" @click="addNewAddress">
            <text class="iconfont icon-add add-icon" :style="{ color: theme.textWhite, fontSize: '36rpx' }"></text>
            <text class="add-text">添加新地址</text>
        </view>
    </view>
</template>

<script>
import Icon from '@/components/common/Icon.vue';
import theme from '@/utils/theme.js';

export default {
    components: {
        Icon
    },
    data() {
        return {
            addresses: [
                {
                    id: 1,
                    name: '张三',
                    phone: '13812345678',
                    detail: '北京市朝阳区建国路88号公寓 3号楼 2单元 303室',
                    isDefault: true
                },
                {
                    id: 2,
                    name: '李四',
                    phone: '13987654321',
                    detail: '北京市海淀区中关村大街1号科技大厦 B座 1001室',
                    isDefault: false
                }
            ],
            theme
        };
    },
    methods: {
        // 返回上一页
        goBack() {
            uni.navigateBack();
        },

        // 添加新地址
        addNewAddress() {
            uni.navigateTo({
                url: '/pages/address/add'
            });
        },

        // 编辑地址
        editAddress(id) {
            uni.navigateTo({
                url: `/pages/address/add?id=${id}`
            });
        },

        // 删除地址
        deleteAddress(id) {
            uni.showModal({
                title: '提示',
                content: '确定要删除该地址吗？',
                success: (res) => {
                    if (res.confirm) {
                        // 模拟删除地址
                        this.addresses = this.addresses.filter(address => address.id !== id);
                        uni.showToast({
                            title: '删除成功',
                            icon: 'success'
                        });
                    }
                }
            });
        },

        // 加载地址列表
        loadAddressList() {
            // 在实际项目中，这里应该通过API请求获取最新的地址列表
            // 模拟从本地存储获取
            uni.getStorage({
                key: 'addressList',
                success: (res) => {
                    this.addresses = res.data;
                },
                fail: () => {
                    // 如果本地没有存储，则使用默认数据
                    // 实际项目中应该从服务器获取
                    uni.setStorage({
                        key: 'addressList',
                        data: this.addresses
                    });
                }
            });
        }
    },
    onLoad() {
        // 这里可以请求后端API获取地址列表数据
        this.loadAddressList();
    },
    // 页面显示时触发，用于从地址添加页面返回时刷新地址列表
    onShow() {
        // 刷新地址列表
        this.loadAddressList();
    }
};
</script>

<style lang="scss">
@import '@/styles/global.scss';

.address-container {
    min-height: 100vh;
    background-color: $bg-color;
    padding-bottom: 120rpx;
}

/* 头部导航 */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20rpx 30rpx;
    background-color: $card-bg;
    position: relative;

    .back-button {
        width: 60rpx;
        height: 60rpx;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .page-title {
        font-size: 36rpx;
        font-weight: bold;
    }

    .placeholder {
        width: 60rpx;
    }
}

/* 地址列表 */
.address-list {
    margin-top: 20rpx;
}

.address-item {
    background-color: $card-bg;
    margin-bottom: 20rpx;
    padding: 30rpx;

    .address-content {
        position: relative;

        .address-header {
            display: flex;
            margin-bottom: 10rpx;

            .address-name {
                font-size: 32rpx;
                font-weight: bold;
                margin-right: 20rpx;
            }

            .address-phone {
                font-size: 28rpx;
                color: $text-gray;
            }
        }

        .address-detail {
            font-size: 28rpx;
            color: $text-black;
            margin-bottom: 10rpx;
            line-height: 1.4;
        }

        .address-tag {
            display: inline-block;
            font-size: 24rpx;
            color: $primary-green;
            border: 1rpx solid $primary-green;
            padding: 4rpx 10rpx;
            border-radius: 6rpx;
        }
    }

    .address-actions {
        display: flex;
        justify-content: flex-end;
        margin-top: 20rpx;
        padding-top: 20rpx;
        border-top: 1rpx solid $border-color;

        .action-item {
            display: flex;
            align-items: center;
            margin-left: 30rpx;

            .action-icon {
                margin-right: 6rpx;
            }

            .action-text {
                font-size: 28rpx;
                color: $text-gray;
            }
        }
    }
}

/* 空状态 */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 100rpx 0;

    .empty-icon {
        font-size: 80rpx;
        margin-bottom: 20rpx;
    }

    .empty-text {
        font-size: 28rpx;
        color: $text-light-gray;
    }
}

/* 添加按钮 */
.add-button {
    position: fixed;
    bottom: 30rpx;
    left: 30rpx;
    right: 30rpx;
    background-color: $primary-green;
    color: $text-white;
    height: 88rpx;
    border-radius: 44rpx;
    display: flex;
    align-items: center;
    justify-content: center;

    .add-text {
        font-size: 32rpx;
        margin-left: 4rpx;
    }
}
</style>